package com.micangcaihang.micang.mvp.task.presenter;

import com.micangcaihang.common.baserx.RxSchedulers;
import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.http.RxHelper;
import com.micangcaihang.common.http.exception.ApiException;
import com.micangcaihang.micang.entity.FileListEntity;
import com.micangcaihang.micang.entity.TaskAccessoryInfoEntity;
import com.micangcaihang.micang.http.MiSubScriber;
import com.micangcaihang.micang.http.request.TaskAccessoryInfoRequest;
import com.micangcaihang.micang.mvp.task.contract.FTaskBorrowerAccessoryContract;
import com.micangcaihang.micang.mvp.task.model.FTaskBorrowerAccessoryModel;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/17
 *     desc   : 风控-借款人附件
 *     version: 1.0
 * </pre>
 */

public class FTaskBorrowerAccessoryPresenter extends FTaskBorrowerAccessoryContract.Presenter {
    private FTaskBorrowerAccessoryModel mModel;
    private String id;
    private String file_type;

    @Override
    public void setV(FTaskBorrowerAccessoryContract.View v) {
        super.setV(v);
        mModel = new FTaskBorrowerAccessoryModel();
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setFile_type(String file_type) {
        this.file_type = file_type;
    }

    /**
     * 每次进入界面获取网络数据，更新界面
     *
     * @param isRefresh 新增参数，用来判断是否为下拉刷新调用，下拉刷新的时候不应该再显示加载界面和异常界面
     */
    @Override
    public void loadFirstData(boolean isRefresh) {
        loadAccessoryInfoData();
    }

    /**
     * 加载更多数据
     */
    @Override
    public void loadMoreData() {

    }

    /**
     * 加载房屋配套数据
     */
    @Override
    public void loadAccessoryInfoData() {
        List<TaskAccessoryInfoEntity> list = new ArrayList<>();
        Observable<ApiResult<FileListEntity>> observable1 = mModel.getAccessoryInfoData(id, "1");//1借款人信息(1借款人信息2借款人婚姻3借款用途相关资料
        Observable<ApiResult<FileListEntity>> observable2 = mModel.getAccessoryInfoData(id, "2");
        Observable<ApiResult<FileListEntity>> observable3 = mModel.getAccessoryInfoData(id, "3");
        mRxManage.add(observable1.concatMap(new Function<ApiResult<FileListEntity>, ObservableSource<ApiResult<FileListEntity>>>() {
                    @Override
                    public ObservableSource<ApiResult<FileListEntity>> apply(@NonNull ApiResult<FileListEntity> listApiResult) throws Exception {
                        if (listApiResult.data != null && listApiResult.data.getFiles() != null) {
                            list.addAll(listApiResult.data.getFiles());
                        }
                        return observable3;
                    }
                }).compose(RxSchedulers.observableIoToMain())
                        .compose(RxHelper.handleResult())
                        .concatMap(new Function<ApiResult<FileListEntity>, ObservableSource<ApiResult<FileListEntity>>>() {
                            @Override
                            public ObservableSource<ApiResult<FileListEntity>> apply(@NonNull ApiResult<FileListEntity> listApiResult) throws Exception {
                                if (listApiResult.data != null && listApiResult.data.getFiles() != null) {
                                    list.addAll(listApiResult.data.getFiles());
                                }
                                return observable2;
                            }
                        }).compose(RxSchedulers.observableIoToMain())
                        .compose(RxHelper.handleResult())
                        .subscribeWith(new MiSubScriber<ApiResult<FileListEntity>>() {
                            @Override
                            protected void doOnError(ApiException e) {
                                mView.showNetError(e);
                            }

                            @Override
                            protected void doOnStart() {
                                mView.showLoading();
                            }

                            @Override
                            protected void doOnNext(ApiResult<FileListEntity> fileListEntityApiResult) {
                                if (fileListEntityApiResult.data != null && fileListEntityApiResult.data.getFiles() != null) {
                                    list.addAll(fileListEntityApiResult.data.getFiles());
                                    mView.returnAccessoryInfoData(list,fileListEntityApiResult.data.getMarriage_status());
                                }
                            }

                            @Override
                            protected void doOnComplete() {
                                mView.showContent();
                            }
                        })
        );
    }

    /**
     * 保存信息
     *
     * @param request
     */
    @Override
    public void saveAccessoryInfoData(TaskAccessoryInfoRequest request) {
        Observable<ApiResult<Void>> observable = mModel.savaAccessoryInfoData(request);
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<Void>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<Void> voidApiResult) {
                mView.showTip("保存成功");
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }
        }));
    }
}
